import random
import os

def output(s,l):
	if l==0:
	   return
	print (s[l-1])
	output(s,l-1)

class mathsCalc:
	def __init__(self,a,b):
		self.a = a
		self.b = b
# 0为运算基数单位的加减法
	def zeroADD(self):
		if self.a + self.b == self.a or self.a + self.b == self.b:
			return str(self.a) + '+' +  str(self.b)
		else:return 'x'
	def zeroMINUS(self):
		if self.a - self.b == self.a:
			return str(self.a) + '-' +  str(self.b)
		else:return 'x'

# 10为运算基数单位的加减法
	def tenMINUS(self):
		if self.a % 10 == 0 and self.b % 10 == 0 and self.a - self.b >= 0:
			return str(self.a) + '-' +  str(self.b)
		else: return'x'
	def tenADD(self):
		if self.a % 10 == 0 and self.b % 10 == 0 and self.a + self.b <= 100:
			return str(self.a) + '+' +  str(self.b)
		else: return'x'

# 10以内的加减法
	def mADD(self,numb):
		if self.a + self.b <= numb:
			return str(self.a) + '+' +  str(self.b)
		else:
			return 'x'
	def mMINUS(self):
		if self.a - self.b >= 0:
			return str(self.a) + '-' +  str(self.b)
		else:
			return 'x'

#100以内的加减乘除
	def mADDPlus(self):
		if self.a + self.b <= 100:
			return str(self.a) + '+' +  str(self.b)
		else:
			return 'x'
	def mMINUSPlus(self):
		if self.a - self.b >= 0:
			return str(self.a) + '-' +  str(self.b)
		else:
			return 'x'
	def mMultiplyPlus(self):
		if self.a * self.b <= 100:
			return str(self.a) + '*' +  str(self.b)
		else:
			return 'x'
	def mDevidePlus(self):
		if self.a % self.b == 0:
			return str(self.a) + '/' +  str(self.b)
		else:
			return 'x'
	def mMethod(self):
		pass

#10和0做基数的加减法
def lowTest():
	zaList = []
	zmList = []
	taList = []
	tmList = []
	while len(zaList) == 0 or len(zaList)<15:
		test = mathsCalc(random.randint(0,11),random.randint(0,11))
		if test.zeroADD() != 'x' and len(zaList) <= 15:
			zaList.append(test.zeroADD()+'=  ')
	while len(zmList) == 0 or len(zmList)<15:
		test = mathsCalc(random.randint(0,11),random.randint(0,11))
		if test.zeroMINUS() !=  'x' and len(zmList) <= 15 :
			zmList.append(test.zeroMINUS()+'=  ')
	while len(taList) == 0 or len(taList)<15:
		test = mathsCalc(random.randint(0,101),random.randint(0,101))
		if test.tenADD() != 'x' and len(taList) <= 15:
			taList.append(test.tenADD()+'=  ')
	while len(tmList) == 0 or len(tmList)<15:
		test = mathsCalc(random.randint(0,101),random.randint(0,101))
		if test.tenMINUS() !=  'x' and len(tmList) <= 15 :
			tmList.append(test.tenMINUS()+'=  ')
	return zaList,zmList,tmList,taList

# 打印出10和0做基数的加减法，并且保存名为zerotenTest.txt的文本在代码所在目录下	
def lowPrint(times):
	tList = []
	filename = os.path.abspath('./zerotenTest.txt')
	for i in range(times):
		test = lowTest()
		tList.append(test[0])
		tList.append(test[1])
		tList.append(test[2])
		tList.append(test[3])
	tList = sum(tList,[])
	num=tList
	L = len(num)
	colNum = []
	for i in range(L):
		with open(filename,'a') as f:
			if (i+1)%times == 0 and i != 0:
				print(num[i].rjust(10,' '))
				print(num[i].rjust(10,' '),file=f)				
			else:
				print(num[i].rjust(10,' '), end=" ")
				print(num[i].rjust(10,' '), end=" ",file=f)

#10以内加减法
def generatTest(numb,selection):
	addList = []
	minusList = []
	if selection == 0:
	# +
		while len(addList) == 0 or len(addList)<30:
			test = mathsCalc(random.randint(1,11),random.randint(1,11))
			if test.mADD(numb) != 'x' and len(addList) <= 30:
				addList.append(test.mADD(numb)+'=  ')
		return addList,[]
	elif selection == 1:
	# -
		while len(minusList) == 0 or len(minusList)<30:
			test = mathsCalc(random.randint(1,11),random.randint(1,11))
			if test.mMINUS() !=  'x' and len(minusList) <= 30:
				minusList.append(test.mMINUS()+'=  ')
		return minusList,[]
	elif selection == 2:
	#+/-
		while len(addList) == 0 or len(addList)<15:
			test = mathsCalc(random.randint(1,11),random.randint(1,11))
			if test.mADD(numb) != 'x' and len(addList) <= 15:
				addList.append(test.mADD(numb)+'=  ')
		while len(minusList) == 0 or len(minusList)<15:
			test = mathsCalc(random.randint(1,11),random.randint(1,11))
			if test.mMINUS() !=  'x' and len(minusList) <= 15:
				minusList.append(test.mMINUS()+'=  ')
	# return ''.join(addList),''.join(minusList)
		return addList,minusList

# 打印出0--10的加减法，并且保存为mTest.txt
def gPrint(numb,times,selection):
	addL = []
	minusL = []
	tList = []
	filename = os.path.abspath('./mTest.txt')
	for i in range(times):
		test = generatTest(numb,selection)
		if len(test[0]) > 0:
			addL.append(test[0])
		if len(test[1]) > 0:
			minusL.append(test[1])
		# tList.append(test[0])
		# tList.append(test[1])
	addL = sum(addL,[])
	minusL = sum(minusL,[])
	for i in range(len(addL)):
		if i == 0 and len(addL)>0 and len(minusL)>0:
			addL.insert(i+1,minusL[i])
		elif i != 0 and len(addL)>0 and len(minusL)>0:
			addL.insert(i+i+1,minusL[i])
	num=addL
	# num=list(filter(None, re.split(' ',''.join(tList))))
	# num = re.split(' ',''.join(tList))
	# num = [i for i in num if i != '']
	L = len(num)
	colNum = []
	for i in range(L):
		with open(filename,'a') as f:
			if (i+1)%times == 0 and i != 0:
				print(num[i].rjust(10,' '))
				print(num[i].rjust(10,' '),file=f)				
			else:
				print(num[i].rjust(10,' '), end=" ")
				print(num[i].rjust(10,' '), end=" ",file=f)
				# colNum.append(num.index(num[i]))

#100以内加减乘除
def bTest():
	addList = []
	minusList = []
	devideList = []
	multiplyList = []
	while len(addList) == 0 or len(addList)<15:
		test = mathsCalc(random.randint(2,99),random.randint(2,99))
		if test.mADDPlus() != 'x' and len(addList) <= 15:
			addList.append(test.mADDPlus()+'=  ')
	while len(minusList) == 0 or len(minusList)<15:
		test = mathsCalc(random.randint(2,99),random.randint(2,99))
		if test.mMINUSPlus() !=  'x' and len(minusList) <= 15 :
			minusList.append(test.mMINUSPlus()+'=  ')
	while len(devideList) == 0 or len(devideList)<15:
		test = mathsCalc(random.randint(2,99),random.randint(2,99))
		if test.mMultiplyPlus() != 'x' and len(devideList) <= 15:
			devideList.append(test.mMultiplyPlus()+'=  ')
	while len(multiplyList) == 0 or len(multiplyList)<15:
		test = mathsCalc(random.randint(2,99),random.randint(2,99))
		if test.mDevidePlus() !=  'x' and len(multiplyList) <= 15 :
			multiplyList.append(test.mDevidePlus()+'=  ')
	# return ''.join(addList),''.join(minusList)
	return addList,minusList,multiplyList,devideList

# 打印出0--100的加减乘除，并且保存为ammdTest.txt
def bPrint(times):
	tList = []
	filename = os.path.abspath('./ammdTest.txt')
	for i in range(times):
		test = bTest()
		tList.append(test[0])
		tList.append(test[1])
		tList.append(test[2])
		tList.append(test[3])
	tList = sum(tList,[])
	num=tList
	# num=list(filter(None, re.split(' ',''.join(tList))))
	# num = re.split(' ',''.join(tList))
	# num = [i for i in num if i != '']
	L = len(num)
	colNum = []
	for i in range(L):
		with open(filename,'a') as f:
			if (i+1)%times == 0 and i != 0:
				print(num[i].rjust(10,' '))
				print(num[i].rjust(10,' '),file=f)				
			else:
				print(num[i].rjust(10,' '), end=" ")
				print(num[i].rjust(10,' '), end=" ",file=f)


if __name__ == "__main__":
	# 参数(几以内,打印行数, 0只有加法，1只有减法，2加减混合)
	gPrint(20,8,0)
	# bPrint(7)
	# lowPrint(7)
		









